package com.music.controller;
/**
 * @className: UserController
 * @description:
 * @author: 林鑫海
 * @date: 2022/7/4
 **/
import com.alibaba.druid.util.StringUtils;
import com.github.pagehelper.PageInfo;
import com.music.dto.Result;
import com.music.entity.ObjVo;
import com.music.entity.User;
import com.music.service.UserFocusService;
import com.music.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

@Controller
@RequestMapping("/user")
public class UserController {
    private static final Logger logger = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private UserService userService;
    @Autowired
    private UserFocusService userFocusService;

    @PostMapping("/reg")
    public @ResponseBody Result register(@RequestParam String userPhone,@RequestParam String userPassword){
        Result result = new Result();
        boolean flag = userService.registerUser(userPhone,userPassword);
        if (flag){
            result.ok(flag);
        }else {
            result.fail(0,"注册失败");
        }
        return result;
    }

    @GetMapping("/Phone")
    public @ResponseBody Result existOfPhoneNumber(@RequestParam String userPhone){
        Result result = new Result();
        boolean flag = userService.existOfPhoneNumber(userPhone);
        return  result.ok(flag);
    }


    @PostMapping("/login")
    public @ResponseBody Result login(@RequestParam String userPhone,@RequestParam String userPassword){
        Result result = new Result();
        User user = userService.login(userPhone,userPassword);
        if(user != null){
            result.ok(user);
        }else {
            result.fail(0,"登录失败");
        }
        return result;
    }

    @GetMapping("/logout")
    public @ResponseBody Result logout(){
        Result result = new Result();
        userService.logout();
        return  result.ok(true);
    }

    @GetMapping("/session")
    public @ResponseBody Result getUserBySession(){
        Result result = new Result();
        User userBySession = userService.getUserBySession();
        if (userBySession!=null){
            result.ok(userBySession);
        }else {
            result.fail(0,"无数据");
        }
        return  result;
    }

    /**
     * 肖泽强
     * @return
     */
    @GetMapping("/back/manage")
    public @ResponseBody Result getUserListToAdmin(@RequestParam String pageNum,@RequestParam String pageSize,String userName){
        Result result = new Result();
        Map<String, Object> map = new HashMap<>();
        if (!StringUtils.isNumber(pageNum) && !StringUtils.isNumber(pageSize)){
           return result.fail(0,"参数错误");
        }
        if(userName != null && !"".equals(userName.trim()))
            map.put("userName",userName);
        map.put("pageNum",pageNum);
        map.put("pageSize",pageSize);
        PageInfo<User> pageInfo = userService.getUserByConditions(map);
        if(Optional.ofNullable(pageInfo.getList()).isPresent()){
            result.ok(pageInfo).setMessage("成功");
        }else {
            result.fail(0,"没有数据");
        }
        return result;
    }

    /**
     * 肖泽强
     * 修改用户信息
     * @param userId
     * @return
     */
    @RequestMapping("/update/manage")
    public @ResponseBody Result updateUserToAdmin(@RequestParam String userId,String status){
        Result result = new Result();
        User user = new User();
        user.setUserId(userId);
        if("正常".equals(status)){
            user.setUserStatus("0");
        }else if("冻结".equals(status)){
            user.setUserStatus("1");
        }
        boolean b = userService.updateUserByKey(user);
        result.ok(b).setMessage(b?"修改成功":"修改失败");
        return result;
    }
    @RequestMapping("/infor")
    public @ResponseBody Result doFindInfor(@RequestParam String userId){
        Result result = new Result();
        List<ObjVo> list=userService.findUserInformationById(userId);
        if(!list.isEmpty()){
            result.ok(list);
        }else{
            result.fail(0,"查询失败");
        }
        return result;
    }
    @RequestMapping("/panduan")
    public @ResponseBody Result doPanDuan(@RequestParam String u1,@RequestParam String u2){
        Result result = new Result();
        int a=userFocusService.panDuanFocus(u1, u2);
        if(a>0){
            result.ok(true);
        }else{
            result.ok(false);
        }
        return result;
    }
    @RequestMapping("/insert")
    public @ResponseBody Result doAdd(@RequestParam String u1,@RequestParam String u2){
        Result result = new Result();
        int a=userFocusService.insert(u1, u2);
        if(a>0){
            result.ok(true);
        }else{
            result.ok(false);
        }
        return result;
    }
    @RequestMapping("/delete")
    public @ResponseBody Result doDelete(@RequestParam String u1,@RequestParam String u2){
        Result result = new Result();
        int a=userFocusService.delete(u1, u2);
        if(a>0){
            result.ok(true);
        }else{
            result.ok(false);
        }
        return result;
    }



}